import 'package:get/get.dart';
import 'package:sports_new/controller/match/detail/match_odds_logic.dart';
import 'package:sports_new/controller/match/detail/zq_match_detail_logic.dart';
import 'package:sports_new/entity/match/zq_odds_entity.dart';
import 'package:sports_new/server/api/soccer_api.dart';

class ZqMatchOddsDetailLogic extends MatchOddsLogic {
  late final detail = Get.find<ZqMatchDetailLogic>(tag: '${Get.arguments[1]}');
  // List<String> typeList = ["欧指", "亚指", "让球", "大小球"];
  // late final TabController tabController;
  List<int> numList = [1001, 1006, 1002, 1007];
  List<List<String>> headTypeList = [
    ["胜", "平", "负"],
    ["主", "盘", "客"],
    ["胜", "平", "负"],
    ["大", "盘", "小"]
  ];
  int typeIndex = 0;
  late List<int> _selectIndex = List.generate(numList.length, (index) => 0);
  bool isLoading = true;
  late List<List<SoccerOddsCompanyEntity>> companyList =
      List.generate(numList.length, (index) => []);
  late List<List<SoccerOddsDetailEntity>> data =
      List.generate(numList.length, (index) => []);

  // List<List<SoccerOddsDetailEntity>> get data => _data;

  // List<List<SoccerOddsCompanyEntity>> get companies => _companies;

  // int get typeIndex => _typeIndex;
  List<int> get selectIndex => _selectIndex;

  @override
  void onInit() {
    typeIndex = Get.arguments[2];
    // tabController =
    //     TabController(length: typeList.length, vsync: this,initialIndex: typeIndex);
    // tabController.addListener(() {
    //   typeIndex = tabController.index;
    // });
    super.onInit();
  }

  void setSelectIndex(int index, int value) {
    if (_selectIndex[index] != value) {
      requestData(value);
    }
    if (value < 0) {
      _selectIndex[index] = 0;
    } else {
      _selectIndex[index] = value;
    }

    update();
  }

  // set typeIndex(int value) {
  //   _typeIndex = value;
  //   update();
  // }

  @override
  void onReady() async {
    await requestCompany();
    for (var i = 0; i < numList.length; i++) {
      await requestData(_selectIndex[i], index: i);
    }
    isLoading = false;
    update();
  }

  Future requestData(int i, {int? index}) async {
    try {
      data[index ?? typeIndex] = await SoccerApi.getZqMatchOddsDetail(
              detail.matchId,
              companyList[index ?? typeIndex][i].id == -1 ||
                      companyList[index ?? typeIndex][i].id == null
                  ? 0
                  : companyList[index ?? typeIndex][i].id!,
              numList[index ?? typeIndex],
              companyList[index ?? typeIndex][i].line ?? "") ??
          [];
    } catch (e) {
      data[typeIndex] = [];
    }
    update();
  }

  Future requestCompany() async {
    for (var i = 0; i < numList.length; i++) {
      companyList[i] = await SoccerApi.getZqMatchOddsCompany(
              detail.matchId, numList[typeIndex]) ??
          [];
      setSelectIndex(
          i,
          companyList[i]
              .indexWhere((element) => element.id == Get.arguments[0]));
    }
    update();
  }
}
